package com.mjh.zhujianju.controller;

import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.utils.XTCurrentUser;
import com.mjh.zhujianju.service.party.BuildingEventSercice;
import com.mjh.zhujianju.service.party.BuildingExamResultService;
import com.mjh.zhujianju.service.party.BuildingExamService;
import com.mjh.zhujianju.util.ApiConstant;
import com.mjh.zhujianju.util.JsonResult;
import com.mjh.zhujianju.vo.party.BuildingEventVO;
import com.mjh.zhujianju.vo.party.BuildingExamResultVO;
import com.mjh.zhujianju.vo.party.BuildingExamVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author liuyang
 * @Description:个人中心
 * @date 2018/9/3 20:27
 */
@RestController
@RequestMapping("/myCenter")
@Api("4、WX->我的")
public class MyCenterController {

    @Autowired
    private BuildingEventSercice buildingEventSercice;

    @Autowired
    private BuildingExamService buildingExamService;

    @Resource
    private BuildingExamResultService buildingExamResultService;

    @Resource
    private SysUserService userService;


    @ApiOperation(value="1 考试查询", notes="查询用户参与的考试记录和结果")
    @RequestMapping(value = "/exam", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getExam(){
        JsonResult result = new JsonResult();
        try {
            Long userId = XTCurrentUser.getCurrentUserId();
            if (null!=userId){
                List<BuildingExamVO> map = buildingExamService.queryBuildingExamByUserIdList(Long.valueOf(userId));
                result.setResult(map);
                result.setStatus(ApiConstant.MSG_SUCCESS);
            }else {
                result.setResult("请先登录");
                result.setStatus(ApiConstant.MSG_NO_USER);
                return ResponseEntity.ok(result);
            }
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="1 考试结果", notes="根据考试id查询结果")
    @RequestMapping(value = "/exam/detail/{id}", method = RequestMethod.GET)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "考试id", required = true, dataType = "Long", paramType = "path")
    })
    public ResponseEntity<JsonResult> getExamDeatil(@PathVariable(value = "id") Long id){
        JsonResult result = new JsonResult();
        try {
            Long userId = XTCurrentUser.getCurrentUserId();
            if (null!=userId){
                Map<String, Object> map = buildingExamResultService.queryBuildingExamResultDeatil(Long.valueOf(userId),id);
                result.setResult(map);
                result.setStatus(ApiConstant.MSG_SUCCESS);
            }else {
                result.setResult("请先登录");
                result.setStatus(ApiConstant.MSG_NO_USER);
                return ResponseEntity.ok(result);
            }
        }catch (XTBusinessException e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }


    @ApiOperation(value="2 活动报名", notes="活动列表")
    @RequestMapping(value = "/event", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getEvent(){
        JsonResult result = new JsonResult();
        try {
            Long userId = XTCurrentUser.getCurrentUserId();
            if (null!=userId){
                Map<String,List<BuildingEventVO>> map = buildingEventSercice.queryBuildingEventList(Long.valueOf(userId));
                result.setResult(map);
                result.setStatus(ApiConstant.MSG_SUCCESS);
            }else {
                result.setResult("请先登录");
                result.setStatus(ApiConstant.MSG_NO_USER);
                return ResponseEntity.ok(result);
            }
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }








}
